"""
或
"""

import pandas as pd
from WindPy import w

from datetime import datetime


def get_data_by_meta(meta):
    # 获取当前日期
    now = datetime.now().strftime("%Y-%m-%d")
    code_to_name = {v[0]:k for k,v in meta.items()} # 代码与中文名映射
    edb_str = ','.join([s[0] for k,s in meta.items()]) # 长字符串
    early_dates = [s[1] for k,s in meta.items()] # 最早日期

    if not w.isconnected():
        w.start()    
    data = w.edb(edb_str, min(early_dates), now) # 一次性下载所有数据
    
    if data.ErrorCode == 0:
        df = pd.DataFrame(data.Data, 
                          index=data.Codes, 
                          columns=data.Times).T
        df.columns = [code_to_name[s] for s in df.columns]
    else:
        print(f"错误码 >> {data.ErrorCode}")
        df = pd.DataFrame()
    return df


if __name__ == "__main__":
    from utils import load_yaml_file
    meta = load_yaml_file("config.yaml")
    df = get_data_by_meta(meta)
    print(df)